import 'package:dk_flutter_base/dk_core.dart';
import 'package:flutter/material.dart';

typedef LogicBtnClickListener();

///参考Android的loadingController，用以在出现网络错误，逻辑错误时替换界面或者对于Widget
///* 定义了四种类型，loading(正在加载)、empty(空)、error(错误)、retryPage(重试)
class LogicPage extends StatelessWidget {
  final LogicParam param;

  LogicPage(this.param) : super();

  LogicPage.emptyPage(
      {LogicParam emptyParam, LogicBtnClickListener btnListener})
      : this(emptyParam ??
            LogicParam.logicEmpty(
                imageUrl: Env?.value?.logicPageConfig?.empty?.imageUrl ?? "",
                tip: Env?.value?.logicPageConfig?.empty?.tip ?? "暂无数据",
                btnContent:
                    Env?.value?.logicPageConfig?.empty?.btnContent ?? "",
                listener: btnListener));

  LogicPage.errorPage(
      {LogicParam errorParam, LogicBtnClickListener btnListener})
      : this(errorParam ??
            LogicParam.logicError(
                imageUrl: Env?.value?.logicPageConfig?.error?.imageUrl ?? "",
                tip: Env?.value?.logicPageConfig?.error?.tip ?? "加载错误",
                btnContent:
                    Env?.value?.logicPageConfig?.error?.btnContent ?? "重试",
                listener: btnListener));

  LogicPage.retryPage(
      {LogicParam retryParam, LogicBtnClickListener btnListener})
      : this(retryParam ??
            LogicParam.logicRetry(
                imageUrl: Env?.value?.logicPageConfig?.retry?.imageUrl ?? "",
                tip: Env?.value?.logicPageConfig?.retry?.tip ?? "网络错误",
                btnContent:
                    Env?.value?.logicPageConfig?.retry?.btnContent ?? "重试",
                listener: btnListener));

  LogicPage.loadingPage(
      {LogicParam loadingParam, Animation<Color> loadingColor})
      : this(loadingParam ??
            LogicParam.loading(
                loadingTxt: Env?.value?.logicPageConfig?.loading?.loadingTxt ??
                    "正在加载...",
                loadingColor:
                    Env?.value?.logicPageConfig?.loading?.loadingColor ??
                        loadingColor));

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Center(child: getChildByType(param)),
    );
  }

  Widget getChildByType(LogicParam param) {
    Widget child;
    switch (param.type) {
      case "empty":
      case "error":
      case "retry":
        child = Column(
            mainAxisAlignment: MainAxisAlignment.center, //对齐方式：平均间隔
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              null != param.imageWidget
                  ? param.imageWidget
                  : stringBlank(param.imageUrl)
                      ? Text("", style: TextStyle(fontSize: 0))
                      : Image.asset(param.imageUrl,
                          width: param.imageWidth ?? 150,
                          height: param.imageHeight ?? 150,
                          fit: BoxFit.contain),
              null != param.tipWidget
                  ? param.tipWidget
                  : stringBlank(param.tip)
                      ? Text("", style: TextStyle(fontSize: 0))
                      : Padding(
                          padding: EdgeInsets.only(top: 14, bottom: 14),
                          child: Text(
                            param.tip,
                            style: param.tipStyle ?? TextStyle(fontSize: 14.0),
                          ),
                        ),
              null != param.btnWidget
                  ? param.btnWidget
                  : stringBlank(param.btnContent)
                      ? Text("", style: TextStyle(fontSize: 0))
                      : OutlineButton(
                          shape: RoundedRectangleBorder(
                              side: BorderSide(
                                  color: Colors.black,
                                  style: BorderStyle.solid,
                                  width: 1),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10))),
                          textColor: Colors.black,
                          highlightColor: Color(0xFF3dbcb6),
                          child: Text(param.btnContent,
                              style: param.btnContentStyle ??
                                  TextStyle(fontSize: 15.0)),
                          padding: EdgeInsets.fromLTRB(15, 10, 15, 10),
                          onPressed: param.listener)
            ]);
        break;
      case "loading":
      default:
        child = Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              SizedBox(
                child: CircularProgressIndicator(
                  valueColor: param.loadingColor,
                  strokeWidth: param.loadingStrokeSize ?? 4,
                ),
                height: param.loadingSize ?? 35.0,
                width: param.loadingSize ?? 35.0,
              ),
              stringBlank(param.loadingTxt)
                  ? Text("", style: TextStyle(fontSize: 0))
                  : Padding(
                      padding: const EdgeInsets.only(
                        top: 20.0,
                      ),
                      child: Text(
                        param.loadingTxt,
                        style:
                            param.loadingTxtStyle ?? TextStyle(fontSize: 12.0),
                      ),
                    )
            ]);
        break;
    }
    return child;
  }
}

///logicPage的配置，可以指定默认的Widget参数、自定义Widget、Widget路由，点击事件
class LogicParam {
  Widget imageWidget;
  String imageUrl;
  double imageWidth;
  double imageHeight;

  Widget tipWidget;
  String tip;
  TextStyle tipStyle;

  Widget btnWidget;
  String btnContent;
  TextStyle btnContentStyle;

  LogicBtnClickListener listener;
  String type;

  String loadingTxt;
  String loadingTxtStyle;
  Animation<Color> loadingColor;
  double loadingSize;
  double loadingStrokeSize;

  LogicParam.logicEmpty(
      {this.imageWidget,
      this.imageUrl,
      this.imageWidth,
      this.imageHeight,
      this.tipWidget,
      this.tip,
      this.tipStyle,
      this.btnWidget,
      this.btnContent,
      this.listener}) {
    this.type = "empty";
  }

  LogicParam.logicError(
      {this.imageWidget,
      this.imageUrl,
      this.imageWidth,
      this.imageHeight,
      this.tipWidget,
      this.tip,
      this.tipStyle,
      this.btnWidget,
      this.btnContent,
      this.btnContentStyle,
      this.listener}) {
    this.type = "error";
  }

  LogicParam.logicRetry(
      {this.imageWidget,
      this.imageUrl,
      this.imageWidth,
      this.imageHeight,
      this.tipWidget,
      this.tip,
      this.tipStyle,
      this.btnWidget,
      this.btnContent,
      this.btnContentStyle,
      this.listener}) {
    this.type = "retry";
  }

  LogicParam.loading(
      {this.loadingTxt,
      this.loadingColor,
      this.loadingTxtStyle,
      this.loadingSize,
      this.loadingStrokeSize}) {
    type = "loading";
  }
}

///为外部提供全局LogicPage的配置，主要用于在网络请求当中
class LogicPageConfig {
  @required
  LogicParam empty;
  @required
  LogicParam error;
  @required
  LogicParam retry;
  @required
  LogicParam loading;

  LogicPageConfig({this.empty, this.error, this.retry, this.loading});
}
